package com.chaychan.bottombarlayout_ohos.slice;

import com.chaychan.bottombarlayout_ohos.HiLogUtils;
import com.chaychan.bottombarlayout_ohos.ResourceTable;
import com.chaychan.library.BottomBarItem;
import com.chaychan.library.BottomBarLayout;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Image;
import ohos.app.dispatcher.task.TaskPriority;

public class ViewPagerDemoActivity extends BaseViewPagerActivity {

    private AnimatorProperty mRotateAnimation;

    @Override
    protected String[] getFragmentContents() {
        return new String[]{"首页", "视频", "微头条", "我的"};
    }

    @Override
    protected int getLayoutResId() {
        return ResourceTable.Layout_ability_view_pager_demo;
    }

    @Override
    protected String getTitle() {
        return "ViewPagerDemoActivity";
    }

    @Override
    public void initListener() {
        super.initListener();
        mBottomBarLayout.setOnItemSelectedListener(new BottomBarLayout.OnItemSelectedListener() {
            @Override
            public void onItemSelected(final BottomBarItem bottomBarItem, int previousPosition, final int currentPosition) {
                HiLogUtils.error("MainActivity", "position: " + currentPosition);
                if (currentPosition == 0) {
                    //如果是第一个，即首页
                    if (previousPosition == currentPosition) {
                        //如果是在原来位置上点击,更换首页图标并播放旋转动画
                        if (mRotateAnimation != null && mRotateAnimation.isRunning()) {
                            //如果当前动画正在执行
                            return;
                        }


                        bottomBarItem.setSelectedIcon(ResourceTable.Media_tab_loading);//更换成加载图标

                        Image bottomImageView = bottomBarItem.getImageView();

                        //播放旋转动画
                        if (mRotateAnimation == null) {
                            mRotateAnimation = bottomImageView.createAnimatorProperty();
                            mRotateAnimation.rotate(360);
                            mRotateAnimation.setDuration(800);
                            mRotateAnimation.setLoopedCount(-1);
                        }
                        mRotateAnimation.start();//播放旋转动画


                        //模拟数据刷新完毕
                        getGlobalTaskDispatcher(TaskPriority.DEFAULT).delayDispatch(new Runnable() {
                            @Override
                            public void run() {
                                getMainTaskDispatcher().syncDispatch(new Runnable() {
                                    @Override
                                    public void run() {
                                        boolean tabNotChanged = mBottomBarLayout.getCurrentItem() == currentPosition; //是否还停留在当前页签
                                        bottomBarItem.setSelectedIcon(ResourceTable.Media_tab_home_selected);//更换成首页原来选中图标
                                        //停止旋转动画
                                        if (mRotateAnimation != null) {
                                            mRotateAnimation.cancel();
                                        }
                                    }
                                });
                            }
                        }, 3000);

                        return;
                    }
                }

                //如果点击了其他条目
                BottomBarItem bottomItem = mBottomBarLayout.getBottomItem(0);
                bottomItem.setSelectedIcon(ResourceTable.Media_tab_home_selected);//更换为原来的图标

                //停止旋转动画
                if (mRotateAnimation != null) {
                    mRotateAnimation.cancel();
                }
            }
        });
    }


}
